/**
 * 所有的事件都掉接口；显示表单验证；根据展示过程来编译代码；触发时间调接口
 */
// 引入路径的文件
// @importScripts("../conmment/MD5.js")
// 切换验证码登录
function changeYZM() {
    document.location.href ='loginTwo.html';
}

// 切换密码登录
function changePassword() {
    document.location.href ='login.html';
}

// 获取时间戳 因为时间戳 和 签名是公共的部分 so 定义Wie全局变量
var newTime = timeAll();
// 倒计时 获取验证码以后开始倒计时
function resetCode() {
    // 先隐藏"获取验证码几个字"
    $('#J_getCode').hide();
    // 然后在页面上显示 60
    $('#J_second').html('60');
    // 与60 同时显示 '秒后发送'
    $('#J_resetCode').show();
    var second = 60;
    var timer = null;
    timer = setInterval(function () {
        second -= 1;
        if (second > 0) {
            $('#J_second').html(second);
        } else {
            clearInterval(timer);
            $('#J_getCode').show();
            $('#J_resetCode').hide();
        }
    }, 1000);
}

// 点击获取验证码；调接口
var isPhone = 1;
function getCode(e) {
    checkPhone(); //验证手机号码
    if (isPhone) {
        var phone = $('#phone').val();
        console.log(phone);
        var timestamp = 'telephone:'+phone+ 'BM'+'timeStamp:'+ newTime +'BM'; // 拼接
        var signs = md5(timestamp);// 加密
        signs = signs.toUpperCase(); // 转化为大写
        $.ajax({
            type: "get",
            url: webUrl3 +"sms/verificationCode",
            data: {
                "telephone": phone,
                "sign": signs,
                "timeStamp": newTime,
            },
            dataType: "JSON",
            async: false,
            success: function (data) {
                console.log(data);
                if (data.status == 200) {
                    alert('发送成功！')
                }else if(data.status == '500'){
                    alert(data.msg);
                }
            }
        });
        resetCode(); // 倒计时(过程先触发接口；在掉及时)
    } else {
        $('#phone').focus();
    }
}


// 验证码 登录接口
function submitBtn() {
    var reg=/^((0\d{2,3}-\d{7,8})|(1[35784]\d{9}))$/;
    var phone = $('#phone').val();
    console.log(phone)
    var code = $('#code').val();
    if (phone.length == undefined){
        alert('手机号不能为空')
    }else if(!reg.test(phone)){
        alert("请输入正确的手机号！");
    }else if(code.length == undefined){
        alert("请输入验证码！");
    }else {
        var timestamp ="telephone:"+phone+ "BM"+"verificationCode:"+code+ "BM" +"timeStamp:"+ newTime + "BM"+"userType:"+"002"+"BM";
        var signs = md5(timestamp);
        signs = signs.toUpperCase();
        console.log(signs)
        console.log(newTime)
        $.ajax({
            type: "get",
            url: webUrl3 + "register/user",
            data: {
                "telephone": phone,
                "verificationCode": code,
                "sign": signs,
                "timeStamp": newTime,
                "userType":'002'
            },
            dataType: "JSON",
            async: false,
            success: function (data) {
                console.log(data);
                if (data.status == 200) {
                    //  保存用户的值；然后在账户管理中展示 没有测试是否保存到了本地的值
                    window.location.href ='index.html';
                } else if(data.status == '500'){
                    alert(data.msg);
                }
            }

        });
    }
}




//  对用户输入手机号码进行验证；如果大于11位就....... 错误情况
$(function () {
    $("#phone").keyup(function () {
        var phone = $(this).val().length;
        if (phone >= 12) {
            $(this).val($(this).val().substring(0, 11));
            alert("手机号不能超过11位");
            return false;
        }
    });
});
//  对用户输入的内容进行的表单验证 错误的情况
$(function () {
    $("#phone").keyup(function () {
        var phone = $(this).val();
        var reg = /^\d+$/; // 通过正则来限制输入的东西
        if (!reg.test(phone)) {
            alert("只能输入数字");
            $(this).val($(this).val().substring(0, 0));
            return false;
        }
    });
});

//验证手机号码
function checkPhone() {
    var phone = $('#phone').val();
    var reg= /^((0\d{2,3}-\d{7,8})|(1[35784]\d{9}))$/;
    isPhone = 1;
    if (phone == '') {
        alert("手机号不能为空！");
        isPhone = 0;
        return;
    }
    if (!reg.test(phone)) {
        alert("请输入正确的手机号！");
        isPhone = 0;
        return;
    }
}



// 密码登录 调接口
function submitBtnTwo() {
    var reg = /^((0\d{2,3}-\d{7,8})|(1[35784]\d{9}))$/;
    var iphone = $('#username').val();
    var passWord = $('#passWord').val();
    passWord = md5(passWord);
    var timestamp ='telephone:'+iphone+ 'BM'+'timeStamp:'+ newTime +'BM'+"password:"+ passWord +'BM';
    var signs = md5(timestamp);
    signs = signs.toUpperCase();
    if (iphone.length == 0) {
        alert('提交的手机号不能为空！')
    }
    else if (!reg.test(iphone)) {
        alert("请输入正确的手机号！");
    }
    else if (passWord.length == 0) {
        alert('提交的密码不能为空！')
    }else {
        $.ajax({
            type: "get",
            url: webUrl3 + "register/storeLogin",
            data: {
                "telephone":iphone,
                "timeStamp": newTime,
                "sign":signs,
                "password":passWord
            },
            dataType: "JSON",
            async: false,
            success: function (data) {
                console.log(data);
                if(data.status == '200'){
                    console.log(data)
                    // var oldPhone = window.localStorage.iphone;
                    window.location.href ='index.html';
                } else if(data.status == '500'){
                    alert(data.msg);
                }

            }
        });
    }
}

